package com.xzj.admin.analysis.base.custom.ty;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: xzj-admin
 * @ClassName: TyCommonHandle
 * @description: 天翼云解析方法
 * @author: XiaZunJun
 * @create: 2024-11-12 22:23
 */
@Slf4j
public class TyCommonHandle {

    /**
     * 虚拟主机子资源
     *
     * @param row 当前解析对象
     * @param parentId 父资源Id
     * @param fieldName 组装子资源的对象字段
     * @param subThemeRows 子资源集合
     */
    protected static void virtualHostSubResource(JSONObject row, String parentId, String fieldName,
                                               JSONArray subThemeRows) {
        if (row.containsKey(fieldName)) {
            String clusterId = row.getString(fieldName);
            JSONObject subRow = new JSONObject();
            subRow.put("id", AnalysisCommonUtil.commonBuildStr(parentId, clusterId));
            subRow.put("parentId", parentId);
            subRow.put("clusterId", clusterId);
            try {
                subThemeRows.add(subRow);
            } catch (Exception e) {
                log.error("添加子资源失败", e);
            }
        }
    }

    /**
     * 虚拟硬盘子资源
     *
     * @param row 当前解析对象
     * @param parentId 父资源Id
     * @param fieldName 组装子资源的对象字段
     * @param subThemeRows 子资源集合
     */
    protected static void virtualVolumeSubResource(JSONObject row, String parentId, String fieldName,
                                                 JSONArray subThemeRows) {
        if (row.containsKey(fieldName)) {
            String uuid = row.getString(fieldName);
            JSONObject subRow = new JSONObject();
            subRow.put("id", AnalysisCommonUtil.commonBuildStr(parentId, uuid));
            subRow.put("parentId", parentId);
            subRow.put("uuid", uuid);
            try {
                subThemeRows.add(subRow);
            } catch (Exception e) {
                log.error("添加子资源失败", e);
            }
        }
    }

    /**
     * 虚拟存储池子资源
     *
     * @param row 当前解析对象
     * @param parentId 父资源Id
     * @param fieldName 组装子资源的对象字段
     * @param subThemeRows 子资源集合
     */
    protected static void virtualStoragePoolSubResource(JSONObject row, String parentId, String fieldName,
                                                      JSONArray subThemeRows) {
        if (row.containsKey(fieldName)) {
            String sn = row.getString(fieldName);
            JSONObject subRow = new JSONObject();
            subRow.put("id", AnalysisCommonUtil.commonBuildStr(parentId, sn));
            subRow.put("parentId", parentId);
            subRow.put("sn", sn);
            subRow.put("specId", "12580002");
            try {
                subThemeRows.add(subRow);
            } catch (Exception e) {
                log.error("添加子资源失败", e);
            }
        }
    }

    /**
     * 物理主机子资源
     *
     * @param row 当前解析对象
     * @param parentId 父资源Id
     * @param fieldName 组装子资源的对象字段
     * @param subThemeRows 子资源集合
     */
    protected static void physicalHostAfterSalesSub(JSONObject row, String parentId,String fieldName, JSONArray subThemeRows) {
        JSONObject afterSalesJson = new JSONObject();
        afterSalesJson.put("parentId", parentId);
        afterSalesJson.put("maintEndDate", row.getString("maintEndDate"));
        afterSalesJson.put("rawValue", row.getString("rawValue"));
        afterSalesJson.put("maintVender", row.getString("maintVender"));
        afterSalesJson.put("id", parentId);
        try {
            subThemeRows.add(afterSalesJson);
        } catch (Exception e) {
            log.error("添加子资源失败", e);
        }
    }



    /**
     * 物理主机子资源
     *
     * @param row 当前解析对象
     * @param parentId 父资源Id
     * @param fieldName 组装子资源的对象字段
     * @param subThemeRows 子资源集合
     */
    protected static void physicalHostPoolListSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        JSONArray poolList = row.getJSONArray(fieldName);
        if (null != poolList) {
            try {
                for (int i = 0; i < poolList.size(); i++) {
                    JSONObject pool = poolList.getJSONObject(i);
                    pool.put("parentId", parentId);
                    pool.put("modelIdentifier", row.getString("modelIdentifier"));
                    pool.put("id", AnalysisCommonUtil.commonBuildStr(parentId, pool.getString("poolId")));
                    subThemeRows.add(pool);
                }
            } catch (Exception e) {
                log.error("添加子资源失败", e);
            }
        }
    }
}
